<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/16  14:57
 */
namespace app\api\logic;
use app\api\BaseModel;
use think\Cache;
use think\Db;
class System extends  BaseModel
{

    //检测参数是否已传
    protected $ParameterDetection  = [
        'createqrcode'=>['goods_id','circle_type'],
    ];

    /**
     * 协议
     */
    public function agreement()
    {
        $where['id'] = 1;
        $where['status'] = 1;
        $result = db('agreement')->where($where)->find();
        return $result;
    }

    /**
    *  购物协议
    */
    public function protocol(){
        $where['id'] = 1;
        $result = db('protocol')->where($where)->find();
        return $result;
    }

    /*
    生成分享圈商品二维码
    */
    public function createqrcode(){
        $data = array(
            'goods_id' => $this->requestData['goods_id'],
            'type' => $this->requestData['circle_type'],
        );
        if(!in_array($data['type'], ['1','2']))return ['message'=>'type参数有误!'];
        if($data['type'] == '1'){
            $circle_goods = db('circle_goods');
        }else{
            $circle_goods = db('circle_goods_demand');
        }
        $qrcode = $circle_goods->where(['id'=>$data['goods_id']])->find();
        if(empty($qrcode)){
            return ['message'=>'circle_type或goods_id参数有误!'];
        }

        if(empty($qrcode['qr_code'])){
            Db::startTrans();
            try {
                $qrcode['qr_code'] = $this->makeQr($data['type'],$data['goods_id']);
                $res = $circle_goods->where(['id'=>$data['goods_id']])->update($qrcode);
                if(!$res) throw new \Exception('生成二维码失败');
                DB::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return array("message"=>$e->getMessage());
            }
        }
        return array("qr_code"=> "https://" . $_SERVER['HTTP_HOST'] . '/static/' . $qrcode['qr_code']);
    }
    /**
     * 二维码
     */
    public function makeQr($circle_type,$goods_id){
        //生成二维码图片
        $img_path_binary = $this->Wechatcode($circle_type,$goods_id);
        $img_name = md5($img_path_binary);

        //$seat_result = db('seat')->where(['qr_code'=>$img_name])->find();
        //if(!empty($seat_result))return $seat_result['qr_code'];

        $this->checkPath(ROOT_PATH.'public\static\code');

        $file = fopen(ROOT_PATH.'public\static\code\\'.$img_name.".png","w");//打开文件准备写入
        fwrite($file,$img_path_binary);//写入
        fclose($file);//关闭

        return 'code/'.$img_name.".png";
    }

    /**
     * 检查目录是否可写
     * @param  string   $path    目录
     * @return boolean
     */
    protected function checkPath($path)
    {
        if (is_dir($path)) {
            return true;
        }
        if (mkdir($path, 0755, true)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *   二维码路径
     */
    public function Wechatcode($circle_type,$goods_id){
        $result =  new \app\api\logic\Wechatcode();
        $result = $result->get_qrcode($circle_type,$goods_id);
        return $result;
    }


}